<template>
    <div class="sidebar">
        <el-menu class="sidebar-el-menu" :default-active="onRoutes"
                 :collapse="collapse" background-color="#324157"
            text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <template v-for="item in menuNodeList">
                <template v-if="item.children">
                    <el-submenu :index="item.id" :key="item.id">
                        <template slot="title">
                            <span slot="title">{{ item.title }}</span>
                        </template>
                        <el-menu-item v-for="child in item.children" :key="child.id" :index="child.route">
                            {{ child.title }}
                        </el-menu-item>
                    </el-submenu>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
import bus from '../common/bus';
import { menuList } from "../../components/axios/url";
import http from "../../components/axios/fetch";
const Cookie = require('js-cookie');

    export default {
        data() {
            return {
                collapse: false,
                current: '', // 菜单默认选择值
                menuNodeList: [],
                parentCurrent: Cookie.get('parentMenuId'),
            }
        },
        created(){
            // 通过 Event Bus 进行组件间通信，来折叠侧边栏
            bus.$on('collapse', msg => {
                this.collapse = msg;
            })
            this.getMenuList()
        },
        methods:{
            getMenuList() {
                try{
                    http.post(menuList, {...this.ruleForm}).then(res => {
                        if(res.data && res.data.resultMess === "success") {
                            this.menuNodeList = res.data.data.menu,
                            this.current = res.data.data.menuId,
                            Cookie.set('parentMenuId', res.data.data.parentMenuId);
                        }else {
                            this.$notify.error({
                                title: '提示',
                                message: res.data.resultMess,
                                type: 'warning'
                            });
                        }
                    })
                } catch(error){
                    this.$notify.error({
                        title: '提示',
                        message: '网络错误',
                    });
                }
            }
        },
        computed:{
            onRoutes(){
                return this.$route.path.replace(Cookie.get('url'),'');
            },
        },
    }
</script>

<style scoped>
    .sidebar{
        display: block;
        position: absolute;
        left: 0;
        top: 50px;
        bottom:0;
    }
    .sidebar-el-menu:not(.el-menu--collapse){
        width: 200px;
    }
    .sidebar > ul {
        height:100%;
    }
</style>
